package com.cie2.janji.pages.users;

import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.hibernate.annotations.CommitAfter;
import org.apache.tapestry5.ioc.annotations.Inject;

import com.cie2.janji.base.CieUserPage;
import com.cie2.janji.entities.Promise;
import com.cie2.janji.entities.Promise.PromiseStatus;
import com.cie2.janji.pages.admin.Admin;
import com.cie2.janji.services.PromiseEngine;
import com.cie2.janji.services.PromiseService;

public class UpdatePromiseStatus extends CieUserPage {

	private Long promiseId;

	@Property
	private Promise promise;
	
	@Property
	private PromiseStatus status; 
	
	//service
	@Inject 
	private PromiseService promiseService;
		
	@Inject
	private PromiseEngine promiseEngine;
	
	Long onPassivate() {
		return promiseId;
	}

	void onActivate(Long promiseId) {
		this.promiseId = promiseId;
		promise = promiseService.load(promiseId);
		status = promise.getStatus();
	}
	
    @CommitAfter
    Object onSuccess()
    {    	    	
    	if( !status.equals(promise.getStatus())) {
    		promiseEngine.updateStatus(status, promise);
    	}
    	
		return PromiseList.class;
		
    }
}
